home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / xconf / xconfig.c < prev   
C/C++ Source or Header  |  1995-01-16  |  4KB  |  199 lines

  1. #include "../misc/misc.h"
  2. #include "xconf.h"
  3. #include "section.h"
  4.  
  5. PUBLIC XCONFIG::XCONFIG()
  6.     :SECTION("","")
  7. {
  8.     //#nbsection = 0;
  9.     parsing.section = NULL;
  10.     parsing.subopt = NULL;
  11.     parsing.subdisp = NULL;
  12. }
  13. /* #Specification: Xconfig / format
  14.     The Xconfig file has a format "mostly" like this
  15.  
  16.     Section "name"
  17.         option [ value ]
  18.         .
  19.     EndSection
  20.         ...
  21.  
  22.     Sometime, this is just a keyword with or without a value
  23.     Sometime, there is no option.
  24.  
  25.     So the way we parsed this is that we only understand the
  26.     keyword for section, and option are seen as subsection themselves.
  27. */
  28.  
  29. /*
  30.     Read a complete Xconfig
  31. */
  32. PUBLIC int XCONFIG::read(const char *fname)
  33. {
  34.     FILE *fin = xconf_fopen (fname,"r");
  35.     int ret = -1;
  36.     if (fin != NULL){
  37.         char buf[500];
  38.         int noline = 0;
  39. //        parsing.section = NULL;
  40. //        parsing.subopt  = NULL;
  41. //        parsing.subdisp = NULL;
  42.         ret = 0;
  43.         while (fgets_strip(buf,sizeof(buf)-1,fin,'\0','\0',&noline)!=NULL){
  44.             char *pt = str_skip (buf);
  45.             int err = 1;
  46.             if (pt[0] == '#'){
  47.                 err = 0;
  48.             }else if (strnicmp("section",pt,7)==0
  49.                 && isspace(pt[7])){
  50.                 pt = str_skip(pt+7);
  51.                 if (*pt == '"'){
  52.                     pt++;
  53.                     char *start = str_skip(pt);
  54.                     while (*pt != '\0' && *pt > ' ') pt++;
  55.                     if (*pt == '"'){
  56.                         *pt = '\0';
  57.                         SECTION *sect = NULL;
  58.                         if (stricmp(start,"monitor")==0){
  59.                             sect = new MONITOR;
  60.                         }else if (stricmp(start,"device")==0){
  61.                             sect = new DEVICE;
  62.                         }else if (stricmp(start,"files")==0){
  63.                             sect = new FILES;
  64.                         }else if (stricmp(start,"keyboard")==0){
  65.                             sect = new KEYBOARD;
  66.                         }else if (stricmp(start,"screen")==0){
  67.                             sect = new SCREEN;
  68.                         }else if (stricmp(start,"pointer")==0){
  69.                             sect = new POINTER;
  70.                         }else if (stricmp(start,"serverflags")==0){
  71.                             sect = new SERVERFLAGS;
  72.                         }
  73.                         if (sect != NULL){
  74.                             err = 0;
  75.                             sect->read (fin,fname,noline);
  76.                             tbsect.add(sect);
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.             if (err){
  82.                 xconf_error ("Invalid line %d in file %s\n%s\n"
  83.                     ,noline,fname,buf);
  84.                 ret = -1;
  85.             }
  86.         }
  87.         fclose (fin);
  88.     }
  89.     return ret;
  90. }
  91. /*
  92.     Sort all section of an XCONFIG. THe section are not sorted, only
  93.     their content.
  94. */
  95. PUBLIC void XCONFIG::sort()
  96. {
  97.     for (int i=0; i<nbvalues; i++){
  98.         tbvalues[i]->sort ();
  99.     }
  100. }
  101. PUBLIC int XCONFIG::print(FILE *fout, int indent)
  102. {
  103.     int ret = 0;
  104.     OPTION *last = NULL;
  105.     for (int i=0; i<nbvalues; i++){
  106.         OPTION *cur = tbvalues[i];
  107.         if (last != NULL && strcmp(cur->keyw,last->keyw)!=0){
  108.             fprintf (fout,"\n\n");
  109.         }
  110.         ret |= cur->print (fout,indent);
  111.         last = cur;
  112.     }
  113.     return ret;
  114. }
  115. PUBLIC COMMENTS::COMMENTS()
  116.     : SECTION("","")
  117. {
  118. }
  119.  
  120. PUBLIC void COMMENTS::print (FILE *fout)
  121. {
  122.     for (int i=0; i<nbvalues; i++){
  123.         OPTION *opt = tbvalues[i];
  124.         fprintf (fout,"#-# %s %s\n",opt->keyw,opt->arg);
  125.     }
  126. }
  127. /*
  128.     Write an Xconfig to a file
  129.     Return -1 if any error.
  130. */
  131. PUBLIC int XCONFIG::write(const char *fname)
  132. {
  133.     int ret = -1;
  134.     FILE *fout = xconf_fopen (fname,"w");
  135.     if (fout != NULL){
  136.         fprintf (fout,"%s",DONT_TOUCH);
  137.         ret = 0;
  138.         comments.print (fout);
  139.         fprintf (fout,"\n");
  140.         print (fout,0);
  141.         fclose (fout);
  142.     }
  143.     return ret;
  144. }
  145.  
  146. /*
  147.     Add/merge an XCONFIG into another
  148.     Section and their content are added to the destination
  149. */
  150. PUBLIC void XCONFIG::merge (const XCONFIG *src)
  151. {
  152.     if (src != NULL){
  153.         SECTION::merge (src);
  154.         comments.merge (&src->comments);
  155.     }
  156. }
  157.  
  158. /*
  159.     Add a comment to the XCONFIG. It will appear at the beginning
  160.     of the Xconfig file with a #-# prefix.
  161. */
  162. PUBLIC void XCONFIG::addcomment(
  163.     const char *keyw,
  164.     const char *manuf_id,
  165.     const char *model_id)
  166. {
  167.     char buf[100];
  168.     sprintf (buf,"%s %s",manuf_id,model_id);
  169.     OPTION *tmpopt = new OPTION (keyw,buf);
  170.     COMMENTS tmpsec;
  171.     tmpsec.addoption (tmpopt);
  172.     comments.merge (&tmpsec);
  173. }
  174.  
  175. /*
  176.     Return the value of the comment identify with keyw
  177. */
  178. PUBLIC const char *XCONFIG::getcomment (const char *keyw)
  179. {
  180.     return comments.findarg (keyw);
  181. }
  182.  
  183. #ifdef TEST
  184.  
  185. int main (int argc, char *argv[])
  186. {
  187.     XCONFIG x;
  188.     x.read (ETC_XF86CONFIG);
  189.     x.sort();
  190.     x.write ("/tmp/XF86Config");
  191.     XCONFIG x2;
  192.     x2.merge (&x);
  193.     x.write ("/tmp/XF86Config2");
  194.     return 0;
  195. }
  196.  
  197. #endif
  198.  
  199.